package com.woniuxy.service.fault.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.*;
import com.woniuxy.mapper.*;
import com.woniuxy.service.fault.FaultService;
import com.woniuxy.service.fix.FixService;
import com.woniuxy.service.indent.IndentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FaultServiceImpl implements FaultService {
    private FaultMapper faultMapper;
    private IndentService indentService;
    private FixService fixAfterSave;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    public FaultServiceImpl(FaultMapper faultMapper, @Lazy IndentService indentService, FixService fixAfterSave) {
        this.faultMapper = faultMapper;
        this.indentService = indentService;
        this.fixAfterSave = fixAfterSave;
    }

    @Override
    public void save(Indent indent) {
        Fault fault=new com.woniuxy.entity.Fault();
        fault.setFaultId(null);
        fault.setBedId(indent.getBedId());
        fault.setFaultName(indent.getFaultName());
        fault.setFaultStatus("故障");
        fault.setFaultTime(new Date());
        fault.setIndentId(indent.getIndentId());
        faultMapper.save(fault);
    }

    /**
     * 故障列表分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Fault> findAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Fault> faults = faultMapper.selectByExample(null);
        //TODO 超级管理员和公司管理员可以查看所有的故障 并分配维修人员
        // 维修人员登录后，只能查看到属于他的维修订单
        PageInfo<Fault> info = new PageInfo<>(faults);
        return info;
    }

    @Override
    public void update(Fault fault) {
        this.fixAfterHandle(fault);
        // 5. 删除 fault 为 已完成的结果
        faultMapper.updateByPrimaryKeySelective(fault);
        this.delete(fault.getFaultId());
        //6.修改床位状态为正常
        bedMapper.returnBedById(fault.getBedId());
    }

    private void fixAfterHandle(Fault fault) {
        // 1. 根据 faultId 查询 indent
        Indent indent = indentService.findByFaultId(fault.getIndentId());
        // 2. 再 2 的记录中获取 user_id
        Fix fix = new Fix();
        fix.setFixId(null);
        fix.setFaultId(fault.getFaultId());
        fix.setFixStartTime(new Date());
        fix.setFixEndTime(new Date());
        fix.setUserId(indent.getUserId());
        fix.setEmpId(fault.getEmpId());
        fixAfterSave.fixAfterSave(fix);
        // TODO 修改 Indent 中的状态
        indentService.updateSuccess(indent.getIndentId());
        //
    }

    @Override
    public Fault findById(Integer faultId) {
        return faultMapper.findById(faultId);
    }

    /**
     * 为故障分配维修人员
     * @param fault
     */
    @Override
    public void distribute(Fault fault) {
        faultMapper.updateByPrimaryKeySelective(fault);
    }

    /**
     * 查询当地所有维修人员
     * @param bedId
     * @return
     */
    @Override
    public List<Emp> findDistributeEmp(Integer bedId) {
        //查到所有维修人员id
        List<Integer> empIds = empMapper.findAllDistributeEmpId(3);


        //查该故障床位所在地
        Bed bed = bedMapper.selectByPrimaryKey(bedId);
        Integer homeId = bed.getHomeId();
        Home home = homeMapper.selectByPrimaryKey(homeId);
        Dept dept = deptMapper.selectByPrimaryKey(home.getDeptId());
        Hospital hospital = hospitalMapper.selectByPrimaryKey(dept.gethId());


        Integer countyId = hospital.getCountyId();
        Integer cityId = hospital.getCityId();
        Integer provinceId = hospital.getProvinceId();

        List<Emp> list = new ArrayList<>();
        for (Integer empId : empIds) {
            System.out.println(empId);
            Emp emp = empMapper.selectByPrimaryKey(empId);
            System.out.println(emp);
            if(emp.getCountyId() == countyId){
                list.add(emp);
            } else if (emp.getCityId() == cityId) {
                list.add(emp);
            } else if (emp.getProvinceId() == provinceId) {
                list.add(emp);
            }
        }

        return list;
    }


    @Override
    public int delete(Integer fId) {
        return faultMapper.deleteByPrimaryKey(fId);
    }
}
